<template>
  <div class="home_container">
    <!-- 搜索导航 -->
    <van-nav-bar
      class="page-nav-bar"
      fixed
    >
      <van-button to="/search" icon="search" class="search_btn" slot="title" round type="info">搜索
      </van-button>
    </van-nav-bar>

    <!-- tabs标签页 -->
    <van-tabs v-model="active" animated swipeable class="home-tabs">
      <van-tab v-for="item in channels" :key="item.id" :title="item.name">
        <article-list :channel="item"></article-list>
      </van-tab>
      <div class="place_holder" slot="nav-right"></div>
      <!-- 汉堡按钮 -->
      <div @click="isShowEditPopup = true" class="hamburger-btn" slot="nav-right">
        <i class="heimatoutiao hmtt-gengduo"></i>
      </div>
    </van-tabs>
    <!-- 编辑频道弹层 -->
    <van-popup
      v-model="isShowEditPopup"
      closeable
      close-icon-position="top-left"
      position="bottom"
      :style="{ height: '90%' }"
    >
    <channel-edit @updateActive="onUpdateActive" :active="active" :my-channels="channels" />
    </van-popup>
  </div>
</template>

<script>
// 导入组件
import { getChannels } from '@/api/user.js'
import ArticleList from './components/articleList'
import ChannelEdit from './components/channelEdit'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage.js'
export default {
  name: 'home',
  data () {
    return {
      active: 0,
      channels: [],
      isShowEditPopup: false
    }
  },

  computed: {
    ...mapState(['user'])
  },

  components: {
    ArticleList,
    ChannelEdit
  },

  created () {
    this.loadChannels()
  },

  methods: {
    // 加载首页频道列表
    async loadChannels () {
      try {
        // 如果用户未登录并且本地还有数据 获取本地数据
        const localChannels = getItem('hmtt_channels')
        if (!this.user && localChannels) {
          this.channels = localChannels
          return false
        }
        const { data: res } = await getChannels()
        this.channels = res.data.channels
      } catch (err) {
        this.$toast.fail('获取频道列表失败')
      }
    },
    onUpdateActive (index, flag) {
      this.active = index
      this.isShowEditPopup = flag
    }
  }
}
</script>
<style scoped lang='less'>
.home_container {
  padding-bottom: 100px;
  // 凡是遇到组件内部的类各样式修改不起作用，在前面加上/deep/
  /deep/.van-nav-bar__title {
    max-width: unset;
  }
  .search_btn {
    width: 555px;
    height: 64px;
    background: rgba(255, 255, 255, .2);
    border-radius: 32px;
  }
  .van-nav-bar .van-icon {
    color: #fff;
  }
  // 标签页的样式覆盖
  /deep/.home-tabs {
    // padding-right: 66px;
    padding-top: 172px;
    .van-tabs__wrap {
      position: fixed;
      top: 92px;
      z-index: 2;
      width: 100%;
    }
    .van-tabs__nav {
      padding-bottom: 0;
      height: 80px;
    }
    .van-tab {
      min-width: 190px;
      border-right: 1px solid #eee;
    }
    .van-tabs__line {
      width: 31px;
      height: 6px;
      background: #3295F9;
      border-radius: 3px;
      bottom: 8px;
    }
    .place_holder {
      width: 66px;
      height: 82px;
      flex-shrink: 0;
    }
    .hamburger-btn {
      width: 66px;
      height: 82px;
      background: #fff;
      position: fixed;
      right: 0;
      text-align: center;
      line-height: 82px;
      opacity: 0.9;
      .hmtt-gengduo {
        font-size: 40px;
      }
      &:before {
        content: '';
        width: 2px;
        height: 100%;
        position: absolute;
        left: 0;
        top: 0;
        background: url('~@/assets/gradient-gray-line.png');
        background-size: contain;
      }
    }
  }
}
</style>
